package com.xss.ipctest.binderpool;

import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.xss.ipctest.R;

public class BinderPoolActivity extends AppCompatActivity {
    private static final String TAG = "BinderPoolActivity";
    private ISecurityCenter mSecurityCenter;
    private ICompute mCompute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_binder_pool);
        new Thread(new Runnable() {
            @Override
            public void run() {
                doWork();
            }
        }).start();
    }

    private void doWork() {
        BinderPool binderPool=BinderPool.getsInstance(BinderPoolActivity.this);
        IBinder securityBinder=binderPool.queryBinder(BinderPool.BINDER_SECURITY_CENTER);
        mSecurityCenter=SecurityCenterImpl.asInterface(securityBinder);
        Log.i(TAG, "doWork: visit ISecurityCenter");
        String msg="helleworld-安卓";
        Log.i(TAG, "doWork: content:"+msg);
        try {
            String password=mSecurityCenter.encrypt(msg);
            Log.i(TAG, "doWork: encrypt:"+password);
            Log.i(TAG, "doWork: decrypt:"+mSecurityCenter.decrypt(password));
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        Log.i(TAG, "doWork: visit ICompute");
        IBinder computeBinder=binderPool.queryBinder(BinderPool.BINDER_COMPUTE);
        mCompute=ComputeImpl.asInterface(computeBinder);
        try {
            Log.i(TAG, "doWork: 3+5="+mCompute.add(3,5));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
